package name.marz.to.go.ahg.SMBC.executer;

import name.marz.to.go.ahg.core.checker.DocumentChecker;
import name.marz.to.go.ahg.core.exception.CannotLoginException;
import name.marz.to.go.ahg.core.exception.HasNotifyMessageException;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class SMBCLoginChecker implements DocumentChecker {
	@Override
	public void check(Document doc) throws CannotLoginException, HasNotifyMessageException {
		// 重大なお知らせがあったら確認メッセージ例外をスロー
		if (doc.html().contains("三井住友銀行からの重要なお知らせ")) {
			String msg = null;
			Elements elems = doc.getElementsByClass("pageNaviLink");
			if ( elems.size() > 0 ) {
				Elements msgs = elems.first().getElementsByTag("p");
				for (Element e : msgs) {
					msg += e.text() + " ";
				}
			}
			throw new HasNotifyMessageException(msg);
		}
		// エラーメッセージを探す
		Elements errs = doc.getElementsByClass("CMNerrorMessage");
		if ( errs == null || errs.isEmpty() ) return;	// エラーメッセージがなければOK
		StringBuffer sb = new StringBuffer();
		for ( Element e : errs ) {
			sb.append(e.text());
		}
		CannotLoginException cle = new CannotLoginException(sb.toString());
		cle.setDocument(doc);
		throw cle;
	}
}
